import org.apache.flink.shaded.guava18.com.google.common.base.Stopwatch;
import org.junit.Test;

import java.util.concurrent.TimeUnit;

/**
 * @Author: zhanglingxing
 * @CreateTime: 2022-07-09
 * @Description: TODO
 * @Version: 1.0
 */
public class StopwatchDemo {

    @Test
    public void test1() throws Exception {
        String orderNo = "12345678";

        System.out.println("订单 [{" + orderNo + "}] 开始处理");
        Stopwatch stopwatch = Stopwatch.createStarted();

        TimeUnit.SECONDS.sleep(1);  // 1秒处理时间

        System.out.println("订单 [{" + orderNo + "}] 处理完成，耗时 [{" + stopwatch.stop() + "}]");
    }

    @Test
    public void test2() throws Exception {
        // 创建stopwatch并开始计时
        Stopwatch stopwatch = Stopwatch.createStarted();
        Thread.sleep(1000);
        // 以秒打印从计时开始至现在的所用时间，向下取整
        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS)); // 1
        // 停止计时
        stopwatch.stop();
        Thread.sleep(2000);
        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS)); // 1

        // 再次计时
        stopwatch.start();
        Thread.sleep(3000);
        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS)); // 2
        // 重置并开始
        stopwatch.reset().start();
        Thread.sleep(4000);

        // 检查是否运行
        System.out.println(stopwatch.isRunning()); // true
        long millis = stopwatch.elapsed(TimeUnit.MILLISECONDS); // 1034
        System.out.println(millis);
        // 打印
        System.out.println(stopwatch.toString()); // 1.034 s
    }

    @Test
    public void test3() throws Exception {

        // 创建stopwatch并开始计时
        Stopwatch stopwatch = Stopwatch.createStarted();
        Thread.sleep(1000);
        // 以秒打印从计时开始至现在的所用时间，向下取整
        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS)); // 1

        // 重置并开始
        stopwatch.reset().start();
        Thread.sleep(2000);
        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS)); // 2

        Thread.sleep(3000);
        // 检查是否运行
        System.out.println(stopwatch.elapsed(TimeUnit.MILLISECONDS)); // 2
        // 打印
        System.out.println(stopwatch.toString()); // 1.034 s

    }
}
